import ttkbootstrap as ttk
from app_gui import ImageProcessorApp

def main():
    # 启用DPI感知
    try:
        from ctypes import windll
        windll.shcore.SetProcessDpiAwareness(1)
    except:
        pass  # 如果失败，继续执行

    root = ttk.Window()

    # 获取屏幕尺寸
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    # 根据屏幕尺寸调整初始窗口大小
    # 使窗口不超过屏幕的80%
    initial_width = min(600, int(screen_width * 0.8))
    initial_height = min(700, int(screen_height * 0.8))

    # 设置窗口初始大小
    root.geometry(f"{initial_width}x{initial_height}")

    # 设置最小窗口大小
    min_width = min(500, int(screen_width * 0.7))
    min_height = min(550, int(screen_height * 0.7))
    root.minsize(min_width, min_height)

    app = ImageProcessorApp(root)

    # 更新窗口以确保所有组件都已正确布局
    root.update()

    # 获取内容的实际大小
    required_width = app.main_frame.winfo_reqwidth()
    required_height = app.main_frame.winfo_reqheight()

    # 确保窗口大小不超过屏幕的90%
    final_width = min(required_width, int(screen_width * 0.9))
    final_height = min(required_height, int(screen_height * 0.9))

    # 调整窗口大小以适应内容
    root.geometry(f"{final_width}x{final_height}")

    # 居中窗口
    x = (screen_width - final_width) // 2
    y = (screen_height - final_height) // 2
    root.geometry(f"{final_width}x{final_height}+{x}+{y}")

    # 允许窗口调整大小
    root.resizable(True, True)

    root.mainloop()


if __name__ == "__main__":
    main()


